package com.bdqn.crm.controller;

import com.bdqn.crm.pojo.SysUser;
import com.bdqn.crm.service.SysUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Controller
public class SysUserController {
    @Resource
    private SysUserService sysUserService;

//    @GetMapping("/hello/{id}")
//    public String getUser(@PathVariable Long id, Model model){
//        SysUser user = sysUserService.getUser(id);
//
//        model.addAttribute("user",user);
//        return "hello";
//    }

    @GetMapping("/inline")
    public String inline( Model model){
        model.addAttribute("userName","ktjiaoyu");
        return "inline";
    }

    @GetMapping("/string")
    public String string(Model model, HttpServletRequest request){
      model.addAttribute("userName","ktjiaoyu");
      request.setAttribute("test","request");
      request.getSession().setAttribute("test","session");
      model.addAttribute("date",new Date());
        return "string";
    }

    @GetMapping("/if")
    public String ifunless( Model model){
        model.addAttribute("flag","man");
        return "if";
    }
     @GetMapping("/list")
     public String list(Model model){
        List<SysUser> users = sysUserService.findAllUsers();
        model.addAttribute("users",users);
        return "list";
     }

     @GetMapping("/index")
    public String index(){
        return "index";
     }
     @GetMapping("/fragment")
    public String layout(){
        return "fragment";
     }

//    @GetMapping("/test")
//    public String main(){
//        return "test";
//    }

    @RequestMapping("/login")
    public String login(){
        return "login";
    }
    @RequestMapping("/main")
    public String main(){
        return "main";
    }
    @RequestMapping("/doLogin")
    public String doLogin(HttpServletRequest request,String usrName,String password){
        SysUser user = sysUserService.login(usrName, password);
        if (user != null ){
            request.getSession().setAttribute("loginUser",user);
            return "main";
        }else
            return "login";
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginUser");
        session.invalidate();
        return "login";
    }

    @GetMapping("/user/list")
    public String userList(String usrName,Long roleId,Model model){
        List<SysUser> users = sysUserService.findAllUsers();
        model.addAttribute("user",users);
        return "/user/list";

    }
}
